#include "historymessagewidget.h"
#include "historymessageitem.h"

HistoryMessageWidget::HistoryMessageWidget(QWidget *parent)
    : QDialog(parent)
{
    //基本逻辑
    this->setWindowTitle("历史消息");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(580,680);
    this->setStyleSheet("QWidget{background-color:rgb(247, 247, 247);}");

    //创建网格布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(20,20,20,20);
    layout->setSpacing(10);
    this->setLayout(layout);

    //创建组件

    //创建单选按钮
    radioBtn1 = new QRadioButton();
    radioBtn2 = new QRadioButton();
    radioBtn1->setText("按关键词搜索");
    radioBtn2->setText("按时间搜索");
    //默认按关键字搜索
    radioBtn1->setChecked(true);

    //创建搜索框与搜索按钮
    searchEdit = new QLineEdit();
    searchBtn = new QPushButton();

    searchEdit->setPlaceholderText("输入搜索内容");
    searchEdit->setStyleSheet("QLineEdit{background-color:rgb(226, 226, 226); padding-left:10px; border:none; border-radius:5px; font-size:16px;} ");
    searchEdit->setFixedHeight(50);
    searchEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    searchBtn->setFixedSize(50,50);
    searchBtn->setIconSize(QSize(50,50));
    searchBtn->setIcon(QIcon(":/image/search.png"));
    QString style = "QPushButton { background-color: rgb(243, 243, 243);border: none; border-radius: 10px;}";
    style += "QPushButton:hover {background-color: rgb(219, 219, 219);} QPushButton:pressed {background-color: rgb(206, 206, 206);}";
    searchBtn->setStyleSheet(style);

    //创建日期选择

    begLabel = new QLabel();
    endLabel = new QLabel();
    begLabel->setText("开始时间");
    endLabel->setText("结束时间");

    begTimeEdit = new QDateTimeEdit();
    begTimeEdit->setFixedHeight(50);
    endTimeEdit = new QDateTimeEdit();
    endTimeEdit->setFixedHeight(50);

    //创建滚动区域
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:2px; background-color: rgb(255, 255, 255); } QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}");
    //scrollArea->setStyleSheet("QScrollArea { border: none;}");

    container = new QWidget();
    scrollArea->setWidget(container);

    //创建垂直布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);
    container->setLayout(vlayout);

    //添加布局
    layout->addWidget(radioBtn1,0,0,1,2);
    layout->addWidget(radioBtn2,0,2,1,2);
    layout->addWidget(searchEdit,1,0,1,8);
    layout->addWidget(searchBtn,1,8,1,1);
    layout->addWidget(scrollArea,2,0,1,9);

    begLabel->hide();
    endLabel->hide();
    begTimeEdit->hide();
    endTimeEdit->hide();

    //切换搜索模式
    connect(radioBtn1,&QRadioButton::clicked,this,[=](){
        //移除原有显示控件
        layout->removeWidget(begLabel);
        layout->removeWidget(endLabel);
        layout->removeWidget(begTimeEdit);
        layout->removeWidget(endTimeEdit);

        begLabel->hide();
        endLabel->hide();
        begTimeEdit->hide();
        endTimeEdit->hide();

        //添加显示控件
        layout->addWidget(searchEdit,1,0,1,8);
        searchEdit->show();
    });

    connect(radioBtn2,&QRadioButton::clicked,this,[=](){
        //移除原有显示控件
        layout->removeWidget(searchEdit);
        searchEdit->hide();

        //添加显示控件
        layout->addWidget(begLabel,1,0,1,1);
        layout->addWidget(begTimeEdit,1,1,1,3);
        layout->addWidget(endLabel,1,4,1,1);
        layout->addWidget(endTimeEdit,1,5,1,3);

        begLabel->show();
        endLabel->show();
        begTimeEdit->show();
        endTimeEdit->show();
    });

    model::DataCenter* dataCenter = model::DataCenter::getInstance();



    /////////////////////////////////////////////////////////////////////
    /// 搜索历史消息业务处理
    /////////////////////////////////////////////////////////////////////
    /// 搜索按钮逻辑
    connect(searchBtn,&QPushButton::clicked,this,&HistoryMessageWidget::clickSearchBtn);




}

void HistoryMessageWidget::addHistoryMessage(const Message &message)
{
    HistoryMessageItem* item = HistoryMessageItem::makeHistoryMessageItem(message);
    container->layout()->addWidget(item);

}

void HistoryMessageWidget::clear()
{
    //获取布局管理器
    QLayout* layout = container->layout();
    for(int i = layout->count() - 1; i>=0 ; --i)
    {
        QWidget* w = layout->itemAt(i)->widget();
        if(w == nullptr) continue;

        layout->removeWidget(w);
        delete w;
    }


}

void HistoryMessageWidget::clickSearchBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    /// 搜索结果返回后界面更新
    connect(dataCenter,&DataCenter::searchMessageDone,this,&HistoryMessageWidget::clickSearchBtnDone);

    //观察单选框的选中情况
    if(radioBtn1->isChecked()) //关键词搜索
    {
        QString text = searchEdit->text();
        dataCenter->searchMessageBySearchKeyAsync(text);
    }
    else //时间搜索
    {
        QDateTime time1 = begTimeEdit->dateTime();
        QDateTime time2 = endTimeEdit->dateTime();
        if(time1 < time2)
        {
            dataCenter->searchMessageByTimeAsync(time1,time2);
        }
        else
        {
            Toast::showMessage("您输入的时间格式有误!");
            return;
        }
    }

}

void HistoryMessageWidget::clickSearchBtnDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QList<Message>* msgList = dataCenter->getSearchMessageResult();
    if(msgList == nullptr)
    {
        LOG() << "获取搜索结果为空";
        return;
    }

    this->clear();

    for(auto& f : *msgList)
    {
        addHistoryMessage(f);
    }

}

















